//
//  YCFindFriendViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/11.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  查找结果

#import "YCFindFriendViewController.h"
#import "YCFriendListCell.h"

@interface YCFindFriendViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (readonly ,nonatomic) UIView *headerView;
@property (readonly ,nonatomic) UILabel *allNumberLabel;
@property (readonly ,nonatomic) UITableView *tableView;
@property (readonly ,nonatomic) NSMutableArray *searchArray;
@property (readonly ,nonatomic) NSString *sex;
@property (readonly ,nonatomic) NSString *age;
@property (readonly ,nonatomic) NSString *province;
@property (readonly ,nonatomic) NSString *city;
@property (readonly ,nonatomic) NSString *area;

@end

@implementation YCFindFriendViewController

@synthesize headerView = _headerView;
@synthesize allNumberLabel = _allNumberLabel;
@synthesize tableView = _tableView;
@synthesize searchArray = _searchArray;

#pragma mark - 属性获取方法

- (NSMutableArray *)searchArray
{
    if (!_searchArray)
    {
        _searchArray = [[NSMutableArray alloc] init];
    }
    
    return _searchArray;
}


- (UIView *)headerView
{
    if (!_headerView)
    {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    
    return _headerView;
}

- (UILabel *)allNumberLabel
{
    if (!_allNumberLabel)
    {
        _allNumberLabel = [[UILabel alloc] init];
        _allNumberLabel.textColor = [UIColor yc_colorWithUInt:0x555555];
        _allNumberLabel.font = [UIFont systemFontOfSize:16];
        _allNumberLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _allNumberLabel;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 80;
        
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.3)];
        _tableView.tableFooterView.backgroundColor = [UIColor yc_colorWithUInt:0xc8c7cc];
    }
    
    return _tableView;
}

#pragma mark - 视图控制器生命周期

- (instancetype)initWithSex:(NSString *)sex Age:(NSString *)age Province:(NSString *)province City:(NSString *)city Area:(NSString *)area
{
    if (self = [super init])
    {
        _sex = sex;
        _age = age;
        _province = province;
        _city = city;
        _area = area;
    }
    
    return self;
}

- (void)dealloc
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"查找结果", nil);
    [self yc_addBackBarButtonItem];
    
    [self layout];
    
    [self searchRequest];
}

#pragma mark - 自定义的方法

- (void)layout
{
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView addSubview:self.allNumberLabel];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    [self.allNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headerView);
        make.left.right.equalTo(self.headerView);
    }];
}

#pragma mark - UITableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.searchArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"IDEN";
    
    YCFriendsListCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell)
    {
        cell = [[YCFriendsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
//    GOCustomerUser *user = self.searchArray[indexPath.row];
    [cell.headerImageView yc_setWebImageWithURLString:@"" holderSize:CGSizeMake(60, 60)];
    cell.nameLabel.text = @"张行";//user.cuNickName;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    GOCustomerUser *user = self.searchArray[indexPath.row];
//    GOShowOffModel *model = [[GOShowOffModel alloc] init];
//    model.userId = user.cuUserId;
//    model.headPhoto = user.cuHeadPhoto;
//    model.nickName = user.cuNickName;
//    model.address = [NSString stringWithFormat:@"%@ %@",user.cuProvince,user.cuCity];
//    GOOstentatiousForOtherViewController *vc = [[GOOstentatiousForOtherViewController alloc] initWithModel:model];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)searchRequest
{
//    [self.tableView.yc_loadingView startAnimating];
    
    NSMutableDictionary *ps = [@{@"pageSize":@(1000),
                                 @"pageIndex":@(0)} mutableCopy];
    
    if (_province)
    {
        ps[@"province"] = _province;
    }
    
    if (_city)
    {
        ps[@"ciyt"] = _city;
    }
    
    if (_area)
    {
        ps[@"area"] = _area;
    }
    
    if (_sex)
    {
        ps[@"sex"] = @([_sex integerValue]);
    }
    
    if (_age)
    {
        ps[@"ageRange"] = _age;
    }
    
//    @go_weakify(self);
//    
//    GOWebServiceSuccessCallBack success = ^(AFHTTPRequestOperation *operation, GOWebServiceResponse *response) {
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self.tableView.go_loadingView stopAnimating];
//        });
//        
//        if(response.success)
//        {
//            @go_strongify(self);
//            if(!self) return;
//            
//            NSArray *listData = response.data;
//            
//            NSError *error;
//            NSArray *objects = [MTLJSONAdapter modelsOfClass:[GOCustomerUser class] fromJSONArray:listData error:&error];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                
//                if(!error)
//                {
//                    _allNumberLabel.text = [NSString stringWithFormat:@"找到%lu位用户",(unsigned long)objects.count];
//                    [self.searchArray addObjectsFromArray:objects];
//                    
//                    [self.tableView reloadData];
//                }
//                
//            });
//        }
//        else
//        {
//            
//        }
//    };
//    
//    GOWebServiceFailureCallBack failure = ^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    };
//    
//    
//    [[GOWebService service] postAction:GOAPISearchUserSearchByCondition
//                                params:ps
//                               success:success
//                               failure:failure];
}


@end
